package com.transaction.service.impl;


import com.transaction.dao.GoodDao;
import com.transaction.dao.OrdDao;
import com.transaction.domain.Good;
import com.transaction.domain.Order;
import com.transaction.service.GoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;

@Service
public class GoodServiceImpl implements GoodService {

    @Autowired
    private GoodDao goodDao;
    @Autowired
    private OrdDao ordDao;

    @Override
    public List<Good> findAllGood() {
        return goodDao.findAllGood();
    }

    @Override
    public Good findOneGood(Good good) {
        return goodDao.findOneGood(good);
    }

    @Override
    public List<Good> findGoodByType(Good good) {
        return goodDao.findGoodByType(good);
    }

    @Override
    @Transactional
    public Boolean order(Order order) {
        int count=0;
        for (String gid:order.getGid()){
            count+=ordDao.insertOrd(order.getUid(),gid);
        }
        if (count == order.getGid().length){
            return true;
        }else {
            return false;
        }
    }

    @Override
    @Transactional
    public Boolean addOneGood(Good good , MultipartFile img , String path) {
        Boolean status = false;
        if (!img.isEmpty()) {
            try {
                File serverFile = new File(path);
                img.transferTo(serverFile);
                int result = goodDao.addOneGood(good);
                if(result== 1)
                    status= true;
                else
                    status= false;
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            status= false;
        }
        return status;
    }

}
